package com.example.newbee.mvp.presenter;

import android.util.Log;

import com.example.mylibrary.mvp.presenter.BasePresenter;
import com.example.newbee.bean.UserBean;
import com.example.newbee.mvp.contact.RegisterContact;
import com.example.newbee.mvp.model.RegisterModel;
import com.example.newbee.mvp.view.RegisterActivity;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class RegisterPresenter extends BasePresenter<RegisterContact.Model, RegisterContact.View> {
    public RegisterPresenter(RegisterContact.Model mModel, RegisterContact.View mView) {
        super(mModel, mView);
    }

    public void login(){
        //判断是否为空
        if(mView.getUsername().length()==0){
            return;
        }
        if(mView.getPwd().length()==0){
            return;
        }
        //登陆
        mModel.login(mView.getUsername(), mView.getPwd(), new Observer<UserBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(UserBean userBean) {
                Log.i("TAG", "onNext: ==========>登陆成功");
                mView.goMain();
            }

            @Override
            public void onError(Throwable e) {
                Log.i("TAG", "onError: ==========登陆>"+e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }

    public void register(){
        //判断是否为空
        if(mView.getUsername().length()==0){
            return;
        }
        if(mView.getPwd().length()==0){
            return;
        }

        //注册
        mModel.register(mView.getUsername(), mView.getPwd(), new Observer<UserBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(UserBean userBean) {
                Log.i("TAG", "onNext: ==========>注册成功");
                mView.goLogin();
            }

            @Override
            public void onError(Throwable e) {
                Log.i("TAG", "onError: ==========>"+e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }

}
